package com.woniuxy.his.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.his.entity.Check;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.his.vo.CheckResultVo;
import com.woniuxy.his.vo.InspectVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import com.woniuxy.his.vo.PayOfCheckVo;
import com.woniuxy.his.vo.PayOfPrescriptionVo;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 检查表 Mapper 接口
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
public interface CheckMapper extends BaseMapper<Check> {

    //多表联合查询 按检查已缴费 和排序字段升序排序
    @Select("select hcase.patient_name,hi.item_name,hc.case_id,hcd.check_detail_id,hc.sort,hc.check_id " +
            "from his_check hc " +
            "join his_check_detail hcd " +
            "join his_item hi " +
            "join his_case hcase " +
            "on hc.check_id = hcd.check_id " +
            "and hc.case_id = hcase.case_id " +
            "and hcd.item_id = hi.item_id " +
            "WHERE hc.ispaid = 'yes' " +
            "and hcd.ischecked = 'no' " +
            "order by hc.sort ") //按缴费顺序排序
    IPage<InspectVo>findAllIspaid(IPage<InspectVo> page);

    //获取影像数据
    @Select("select * from his_image_check where case_id in (select case_id from his_case where patient_id in (select patient_id from his_guahao where gua_id=#{input})) order by image_add_time desc limit 0,1")
    public CheckResultVo getImage(Integer input);

    //获取血液数据
    @Select("select * from his_blood_check where case_id in (select case_id from his_case where patient_id in (select patient_id from his_guahao where gua_id=#{input})) order by rwa_add_time desc limit 0,1")
    public CheckResultVo getBlood(Integer input);


    //获取尿检数据
    @Select("select * from his_piss_check where case_id in (select case_id from his_case where patient_id in (select patient_id from his_guahao where gua_id=#{input})) order by check_add_time desc limit 0,1")
    public CheckResultVo getPee(Integer input);


    //获取病例ID
    @Select("select case_id from his_case where patient_id in (select patient_id from his_guahao where gua_id=#{guaId})")
    public Integer getCaseId(Integer guaId);

    //写入检查表
    @Insert("insert into his_check (case_id,total_price) values (#{caseId},#{totalProject})")
    public Integer insertCheck(Integer totalProject, Integer caseId);

    //获取检查表ID
    @Select("select max(check_id) from his_check where case_id=#{caseId}")
    public Integer getCheckId(Integer caseId);

    //动态获取项目ID
    @Select("<script>select item_id from his_item where item_name in <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>#{item}</foreach></script>")
    public List<Integer> getItemId(@Param("list") List<String> list);

    //写入检查详情表
    @Insert("insert into his_check_detail (item_name,doctor_id,check_price,check_id,item_id) values (#{itemName},#{doctorId},#{checkPrice},#{checkId},#{itemId})")
    public Integer insertCheckDetail(String itemName, Integer doctorId, Double checkPrice, Integer checkId, Integer itemId);


    //获取项目已缴费患者
    @Select("SELECT hck.check_id,hc.patient_name from his_check hck,his_case hc where hck.ispaid = 'no' and hck.case_id=hc.case_id and hck.rwa_add_time BETWEEN #{startTime} and #{stopTime}")
    IPage<PayOfCheckVo> getAllCheckPayMassage(IPage<PayOfCheckVo> page, Date startTime, Date stopTime);
}
